package com.kalix.jyksh.proint.biz;

import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.ksh.proint.api.biz.ILineChartRequestBeanService;
import com.kalix.ksh.proint.api.dao.ILineChartRequestBeanDao;
import com.kalix.ksh.proint.api.model.PostRequestUtil;
import com.kalix.jyksh.proint.entities.LineChartRequestBean;
import com.kalix.jyksh.proint.entities.LineChartTitleBean;
import org.json.JSONArray;
import org.json.JSONObject;


import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


public class LineChartRequestBeanServiceImpl extends GenericBizServiceImpl<ILineChartRequestBeanDao, LineChartRequestBean> implements ILineChartRequestBeanService {
    public LineChartRequestBeanServiceImpl(){
        super.init(LineChartRequestBean.class.getName());
    }


    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String wsPort =(String)ConfigUtil.getConfigProp("FREEBOARD_POST","config.ksh.portconf");

    /**
     * 获取折线图服务的数据
     * @return
     */
    public String getValue() {

        String sql = "select li from LineChartRequestBean li";
        List<LineChartRequestBean> list = dao.find(sql);
        List<LineChartTitleBean> l = getListTitle();
        long ms = System.currentTimeMillis();
        long cache_time = ms - 90000000;
        long start_absolute = ms - 3000000;
        JSONObject jObject = new JSONObject();
        JSONArray jsonArrays = new JSONArray();
        JSONArray ja = new JSONArray();
        String one = "";

        int number = Integer.parseInt(l.get(0).getMax());
        for (int i = 0; i < number; i++) {
            // 取数据
            String uuid = list.get(i).getUuid();
            String strJson = "{" + "\"metrics\":[" + "{" + "\"tags\":{" +
                    "\"mo\":[" + "\"Line.domain=\\\"defaultEngine\\\",uuid=\\\"" + uuid + "\\\"\"" +
                    "]" + "}," + "\"name\":\"ifOctets\"" + "}" + "]," +
                    "\"cache_time\":" + cache_time + "," + "\"start_absolute\":" + start_absolute + "}";
            String json = PostRequestUtil.HttpPostWithJson("http://" + wsIp + ":" + wsPort + "/api/v1/datapoints/query", strJson);
            JSONObject jsonObject = new JSONObject(json);
            JSONArray js = jsonObject.getJSONArray("queries");
            JSONObject jj = js.getJSONObject(0);
            JSONArray ss = jj.getJSONArray("results");
            JSONObject s = ss.getJSONObject(0);
            JSONArray arr = s.getJSONArray("values");
            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObject1 = new JSONObject();
            for (int j = 0; j < number ; j++) {
                JSONArray jarr =  arr.getJSONArray(j);
                one = String.valueOf((jarr.getLong(1) / 1024));
                jsonArray.put(one);
            }
            // 数据格式data中的
            jsonObject1.put("value",jsonArray);
            jsonObject1.put("name",list.get(i).getName());
            jsonArrays.put(jsonObject1);

            //数据格式xData中的
            String data = "";
            if(i == 0){
                data = "当前";
                ja.put(data);
            }else {
                data = "前"+i*10+"分钟";
                ja.put(data);
            }
        }

        jObject.put("data",jsonArrays);
        jObject.put("xData",ja);
        jObject.put("PrivateName",l.get(0).getTitle());

        return jObject.toString();
    }

    /**
     * @Description 截取double类型数据小数点后两位小数
     * @auther:
     * @date: 2018/10/29 0029 上午 11:32
     */
    public String  getString(double dou){
        DecimalFormat df   = new DecimalFormat("##########0.00");
        return df.format(dou);
    }

/**
 * * 功能描述: 获取折线图标题
 * @User wanghw
 * @Date 下午 3:03 2018/11/5 0005
 * @Param 
 * @return 
 */

    public List<LineChartTitleBean> getListTitle(){
        String sql = "select lt from LineChartTitleBean lt";
        List<LineChartTitleBean> list = dao.find(sql);
        return list;
    }
}

